/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iftk.xmlparser.domain;

import java.util.List;

/**
 *
 * @author treiman
 */
public class Course extends BubbleItem {
    private String nimi;
    private String taso;
    private String linja;
    private String koodi;
    private List<Event> tapaht;

    public String getKoodi() {
        return koodi;
    }
    
    public void setKoodi(String koodi) {
        this.koodi = koodi;
    }

    public String getLinja() {
        return linja;
    }

    public void setLinja(String linja) {
        this.linja = linja;
    }

    public String getNimi() {
        return nimi;
    }

    public void setNimi(String nimi) throws Exception {
        this.nimi = nimi;
    }

    public List<Event> getTapaht() {
        return tapaht;
    }

    public void setTapaht(List<Event> tapaht) {
        this.tapaht = tapaht;
    }

    public String getTaso() {
        return taso;
    }

    public void setTaso(String taso) {
        this.taso = taso;
    }
    
    @Override public String toString() {
        StringBuilder result = new StringBuilder();
        String NEW_LINE = System.getProperty("line.separator");

        result.append(this.getClass().getName()).append(" Object {").append(NEW_LINE);
        result.append(" ID: ").append(getId()).append(NEW_LINE);
        result.append(" Name: ").append(nimi).append(NEW_LINE);
        result.append(" Level: ").append(taso).append(NEW_LINE);
        result.append(" Program: ").append(linja).append(NEW_LINE);
        result.append(" Course code: ").append(koodi).append(NEW_LINE);
        //Note that Collections and Maps also override toString
        result.append(" Events: ").append(NEW_LINE);
        for(Event e : getTapaht()) {
            result.append(e.toString()).append(NEW_LINE);
        }
        result.append(" Links: ");
        for(Integer i : getLinks()) {
            result.append(i).append("\t");
        } 
        result.append(NEW_LINE);
        
        result.append("}");

        return result.toString();
    }
    
    @Override
    public String toBubble() {
        StringBuilder result = new StringBuilder();
        String NEW_LINE = System.getProperty("line.separator");
        String program;
        boolean isInEnglish = false;
        boolean isInFinnish = false;
        
        if(linja.equals("T")) {
            program = "Networking and Services";
        }
        else if(linja.equals("H")) {
            program = "Software Systems";
        }
        else if(linja.equals("A")) {
            program = "Algorithms and machine learning";
        }
        else {
            program = "Does not belong to a specific program";
        }
        
        result.append("<bubble>").append(NEW_LINE);
        result.append("\t<id>").append(getId()).append("</id>").append(NEW_LINE);
        result.append("\t<titletext>").append(nimi).append("</titletext>").append(NEW_LINE);
        result.append("\t<titlefontsize>10</titlefontsize>").append(NEW_LINE);
        result.append("\t<shorttext fontsize=\"3\">").append(nimi).append(NEW_LINE).
                append(program).append(NEW_LINE).append(NEW_LINE);
        /*for(Event e : this.getTapaht()) {
            result.append(e.getPaiva()).append(" ").append(e.getA_aika()).
                    append("-").append(e.getL_aika()).append(", ").append(e.getSali()).
                    append(" ").append(e.getVetaja()).append(NEW_LINE);
        }*/
        for(int i = 0; i < this.getTapaht().size(); i++) {
            if(this.getTapaht().size() > 0) {
                if(i == this.getTapaht().size()-1) {
                    result.append(getTapaht().get(i).getPaiva()).append(" ").append(getTapaht().get(i).getA_aika()).
                            append("-").append(getTapaht().get(i).getL_aika()).append(", ").append(getTapaht().get(i).getSali()).
                            append(" ").append(getTapaht().get(i).getVetaja());                                        
                }
                else {
                    result.append(getTapaht().get(i).getPaiva()).append(" ").append(getTapaht().get(i).getA_aika()).
                            append("-").append(getTapaht().get(i).getL_aika()).append(", ").append(getTapaht().get(i).getSali()).
                            append(" ").append(getTapaht().get(i).getVetaja()).append(NEW_LINE);                    
                }
            }
        }
        result.append("</shorttext>").append(NEW_LINE);
        
        // determine the language of the course if there are events based on which
        // to determine such a thing
        if(tapaht != null && tapaht.size() > 0) {
            for(Event e : tapaht) {
                if(e.getTyyppi().equals("LU") && e.getKieli().equals("E")) {
                    isInEnglish = true;
                }
                else if(e.getTyyppi().equals("LU") && e.getKieli().equals("S")) {
                    isInFinnish = true;
                }
                else if(e.getTyyppi().equals("LH") && e.getKieli().equals("E")) {
                    isInEnglish = true;
                }
                else if(e.getTyyppi().equals("LH") && e.getKieli().equals("S")) {
                    isInFinnish = true;
                }
            }            
        }
        
        if(isInEnglish) {
            result.append("\t<style>English</style>").append(NEW_LINE);
        }
        else if(isInFinnish) {
            result.append("\t<style>Finnish</style>").append(NEW_LINE);
        }
        else {
            result.append("\t<style>EitDarkCyan</style>").append(NEW_LINE);
        }
        result.append("\t<links>");
        for(int i = 0; i < getLinks().size(); i++) {
            if(i == getLinks().size()-1 && getLinks().size() > 0) {
                result.append(getLinks().get(i));
            }
            else {
                result.append(getLinks().get(i)).append(",");
            }
        }
        result.append("</links>").append(NEW_LINE);
        result.append("<priority>3</priority>").append(NEW_LINE);
        result.append("</bubble>").append(NEW_LINE);
        
        return result.toString();
    }
    
}
